package net.helix.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.helix.engine.EngineTop;
import net.infonode.docking.RootWindow;

public class UIEngineTop {
	/**
	 * The main window's frame.
	 */
	public static JFrame frame;
	/**
	 * The tabbedPane that goes around every project and also the welcome screen.
	 */
	private static JTabbedPane mainTabbedPane;
	/**
	 * The file, edit, ect menu.
	 */
	public static JMenuBar menu;
	/**
	 * Crate the main window's JFrame object and add closing events. Also create mainTabbedPane which every project gets added to.
	 */
	public static void Start(){
		// gets rid of JPanel flickering on some computers (like my windows XP)
		System.setProperty("sun.awt.noerasebackground", "true");
		
		// TODO: add window's icon
		frame = new JFrame("Helix");
		//JFrame.setDefaultLookAndFeelDecorated(true);
		
		try {
			// Choose lookAndFeel that is closest to the OS you are using.
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Create the main menu bar
		setMenu(getDefaultMenu());
		//frame.setVisible(true);
		
		frame.setMinimumSize(new Dimension(400, 300));
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		Dimension setDimension = new Dimension(400, 300);
		while(screenSize.width > setDimension.width && screenSize.height > setDimension.height + 400){
			setDimension.width += 400;
			setDimension.height += 300;
		}
		frame.setSize(setDimension);
		frame.setPreferredSize(setDimension);
		
		// TODO: add X buttons to the tabs.
		mainTabbedPane = new JTabbedPane();
		
		frame.add(mainTabbedPane);
		
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		frame.addWindowListener(new WindowListener(){

			@Override
			public void windowActivated(WindowEvent arg0) {
				
			}
			@Override
			public void windowClosed(WindowEvent arg0) {
				System.out.println("Window closed");
				
				System.exit(0);
			}
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.out.println("Window closing");
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				
			}
			
		});
		
		/*
		try {
			// Choose lookAndFeel that is closest to the OS you are using.
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		*/
		
	}
	public static void setMenu(JMenuBar newMenu){
		menu = newMenu;
		frame.setJMenuBar(menu);
	}
	
	public static JMenu getDefaultMenuFile(){
		JMenu defaultMenuFile = new JMenu("File");
			
		JMenuItem newProject = new JMenuItem("New Project");
		newProject.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				
				EngineTop.newProject();
			}
		});
		
		defaultMenuFile.add(newProject);
		defaultMenuFile.add("Load Project");
		defaultMenuFile.add("Quit");
		
		return defaultMenuFile;
		
	}
	public static JMenu getDefaultMenuHelp(){
		JMenu defaultMenuHelp = new JMenu("Help");
		defaultMenuHelp.add("Tutorials");
		defaultMenuHelp.add("Wiki");
		defaultMenuHelp.add("Offline Documentation");
		defaultMenuHelp.addSeparator();
		defaultMenuHelp.add("About");
		
		return defaultMenuHelp;
	}
	
	private static JMenuBar defaultMenu = null;
	public static JMenuBar getDefaultMenu(){
		if(defaultMenu == null){
			defaultMenu = new JMenuBar();
			
			defaultMenu.add(getDefaultMenuFile());
			defaultMenu.add(getDefaultMenuHelp());
		}
		return defaultMenu;
	}
	public static void AddTabPane(String name, RootWindow rootWindow){
		mainTabbedPane.addTab(name, rootWindow);
		
		mainTabbedPane.setTabComponentAt(mainTabbedPane.getTabCount()-1, new ButtonTabComponent(mainTabbedPane));
		
		
	}
	
	
}
